﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class HR_CreateNewUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (!Page.IsPostBack)
            {
                BindEmployees();
            }
        }
    }
    public void SaveNewUser()
    {
        using (POSEntities db = new POSEntities())
        {
            User user = new User();
            try
            {
                user.EmpId = Convert.ToInt32(ddl_employee.SelectedValue);
                user.UserName = txtUserName.Text;
                user.Password = txtPassword.Text;
                user.ExpiryDate = DateTime.Now.AddDays(Convert.ToInt32(DDL_Expiry.SelectedValue));
                user.CreatedOn = DateTime.Now;
                user.Is_Active = true;
                db.Users.Add(user);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

            }

        }
    }

    private void BindEmployees()
    {
        using (POSEntities db = new POSEntities())
        {
            int branchId = 3;
            try
            {
                var employees =(from c in db.Employees where !(from u in db.Users select u.EmpId).Contains(c.EmpId) select c).ToList();
                ddl_employee.DataSource = employees;
                ddl_employee.DataBind();
            }
            catch (Exception ex)
            {

            }

        }
    }

    protected void btnCreateUser_Click(object sender, EventArgs e)
    {
        SaveNewUser();
        BindEmployees();
        txtUserName.Text = "";
    }
}